package com.Mobile.Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.Entity.Userdata;
import com.Entity.Users;
import com.Service.UserdataService;


public class MobileCalculateWeekAction implements ServletResponseAware {

	/**
	 * @param args
	 */
	private HttpServletResponse response;//response 作用
	private int flag=0;
	public static long CONST_WEEK = 3600 * 1000 * 24 * 7;
	private Date expecteddateBirth;
	private int userid=0;
	@Autowired
	private UserdataService userdataService;
	
	
	public UserdataService getUserdataService() {
		return userdataService;
	}
	public void setUserdataService(UserdataService userdataService) {
		this.userdataService = userdataService;
	}

	@SuppressWarnings("null")
	public void calculate() throws ServletException,IOException{
		// TODO Auto-generated method stub
		
		String userId=ServletActionContext.getRequest().getParameter("userId");
		String pregnancydate1=ServletActionContext.getRequest().getParameter("pregnancyDate");
		String expecteddatebirth=ServletActionContext.getRequest().getParameter("expectedDateBirth");
		
		userId=java.net.URLDecoder.decode(userId,"UTF-8");
		pregnancydate1 = java.net.URLDecoder.decode(pregnancydate1,"UTF-8");
		expecteddatebirth = java.net.URLDecoder.decode(expecteddatebirth,"UTF-8");
		
		JSONObject returnjson=new JSONObject();				
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8"); 
		PrintWriter out = response.getWriter();	
		
			if(pregnancydate1!=null&&!pregnancydate1.equals("")&&!pregnancydate1.equals("null")){
				System.out.println("开始喽");				
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				Date pregnancydate2 = null;				
					try {
						pregnancydate2 = df.parse(pregnancydate1);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}				
				Date date = new Date();
				System.out.println();
				long betweenSeconds = (date.getTime() - pregnancydate2.getTime()) / 1000;// 闂勩倓浜?000閺勵垯璐熸禍鍡氭祮閹广垺鍨氱粔?			
				long expecteddate=(pregnancydate2.getTime()/(24*60*60*1000)+1+280)*(24*60*60*1000);			
				String times = df.format(new Date((long) expecteddate));  
			    System.out.print("日期格式OK---->" + times);  			    
			   
			   // SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
				Date dt = new Date(expecteddate);  
			    userid=Integer.parseInt(userId);
			    System.out.println("userid"+userid);
			    expecteddateBirth=dt;
			    System.out.println("expectedDateBirth"+expecteddateBirth);
			   
				
			    //閸掋倖鏌囩拠顓炲綖 
				double betweendays = betweenSeconds / (24 * 3600);
				int weeks = (int) Math.floor(betweendays/7);
				if(weeks<0||weeks>41){
					flag=0;
					
					returnjson.put("flag", flag);									
				}else{
					//閸欐垿鈧汞son
					
					this.userdataService.updateExpectedDateBirth(expecteddateBirth, userid);
					flag=1;
					returnjson.put("flag", flag);																				
					returnjson.put("weeks",weeks);
					returnjson.put("expecteddateBirth", times);
					
					
				}
				
				
			}else if(expecteddatebirth!=null){
				System.out.println("另一个if");
				flag=1;
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				Date expecteddateBirth1 = null;
				userid=Integer.parseInt(userId);
					try {
						expecteddateBirth1 = df.parse(expecteddatebirth);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				Date date = new Date();
				
				double betweenSeconds = (expecteddateBirth1.getTime()-date.getTime() ) / 1000;// 闂勩倓浜?000閺勵垯璐熸禍鍡氭祮閹广垺鍨氱粔?
				double betweendays = betweenSeconds / (24 * 3600);
				int weeks = (int) Math.floor(40-betweendays/7);
				if(weeks<0||weeks>41){
					flag=0;
					
					returnjson.put("flag", flag);																				
					
					
				}else{
					flag=1;
					returnjson.put("flag", flag);																				
					returnjson.put("weeks",weeks);
					returnjson.put("expecteddateBirth", expecteddatebirth);
					expecteddateBirth=expecteddateBirth1;
					this.userdataService.updateExpectedDateBirth(expecteddateBirth, userid);
				}
				
			}/*else{
				returnjson.put("flag", flag);
		}*/
		
		
		out.println(returnjson.toString());
		out.flush();
		out.close();
	}
	
	
	
	@Override
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response=response;
	}
	public Date getExpecteddateBirth() {
		return expecteddateBirth;
	}
	public void setExpecteddateBirth(Date expecteddateBirth) {
		this.expecteddateBirth = expecteddateBirth;
	}
	public int getUserid() {
		return userid;
	}
	public void setUserid(int userid) {
		this.userid = userid;
	}
	
	

}
